package com.ruoyi.business.api;

import com.google.common.collect.Lists;
import com.ruoyi.business.request.ApiTaskReq;
import com.ruoyi.business.request.ApiTaskSubmitReq;
import com.ruoyi.business.request.ApiUploadTaskAnnexReq;
import com.ruoyi.business.request.TaskCreateReq;
import com.ruoyi.business.response.*;
import com.ruoyi.business.service.ITaskAnnexService;
import com.ruoyi.business.service.ITaskService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.PageResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author liutao
 * @version 1.0
 * @description: APP任务接口
 * @date 2024/3/18 00:13
 */

@RestController
@RequestMapping("/api/task")
public class TaskApi {
    @Resource
    private ITaskService taskService;
    @Resource
    private ITaskAnnexService taskAnnexService;

    @GetMapping("/page")
    public R<PageResult<ApiTaskRes>> taskList(ApiTaskReq req) {
        return R.ok(taskService.pageTaskList(req));
    }

    /**
     * 获取任务点位列表
     * @return
     */
    @GetMapping("/point/list")
    public R<List<ApiTaskPointRes>> getTaskPointList(@RequestParam Long taskId) {
        return R.ok(taskService.getTaskPointApi(taskId));
    }

    /**
     * 获取用户任务点位列表
     * @return
     */
    @GetMapping("/user/point/list")
    public R<List<ApiTaskPointRes>> getTaskUserPointList(Long researchPlanId) {
        if (researchPlanId == null) {
            return R.ok(Lists.newArrayList());
        }
        return R.ok(taskService.getTaskUserPointApi(researchPlanId, SecurityUtils.getUserId()));
    }

    /**
     * 获取任务点位详情
     * @return
     */
    @GetMapping("/point/info")
    public R<ApiTaskPointInfoRes> getTaskPointInfo(@RequestParam Long taskId, @RequestParam Long pointId) {
        return R.ok(taskService.getTaskPointInfo(taskId, pointId));
    }

    /**
     * 上传任务调研附件
     * @param req 参数
     * @return
     */
    @PostMapping(value = "/upload/annex", consumes = {"multipart/form-data"})
    public R<Map<String, Object>> uploadTaskAnnex(ApiUploadTaskAnnexReq req) {
        return R.ok(taskAnnexService.uploadTaskAnnex(req));
    }
    /**
     * 上传任务调研附件
     * @param id 附件id
     * @return
     */
    @PostMapping(value = "/delete/annex/{id}")
    public R<Boolean> deleteTaskAnnex(@PathVariable Long id) {
        return R.ok(taskAnnexService.deleteTaskAnnex(id));
    }

    /**
     * 根据点位获取调研附件
     * @param taskPointId 任务点位id
     * @return
     */
    @GetMapping("/annex/{taskPointId}")
    public R<List<ApiTaskAnnexRes>> listTaskAnnex(@PathVariable Long taskPointId) {
        return R.ok(taskAnnexService.listTaskAnnex(taskPointId));
    }


    /**
     * 提交任务调研
     * @param req
     * @return
     */
    @PostMapping("/submit")
    public R<Boolean> submit(@RequestBody @Validated ApiTaskSubmitReq req) {
        return R.ok(taskService.submitSurveyTask(req));
    }

    @PostMapping("/add")
    public R<Boolean> add(@RequestBody @Validated TaskCreateReq req) {
        req.setSource(1);
        Long[] userIds = {SecurityUtils.getUserId()};
        req.setUserIds(userIds);
        return R.ok(taskService.createTask(req));
    }
}
